package com.forlove.app.qselect.friend.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.forlove.app.R;
import com.forlove.app.qselect.friend.entity.SelectEntity;
import com.forlove.app.qselect.login.entity.SchoolInfoEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yjl on 2017/11/27.
 */

public class SelectSchoolTab extends RelativeLayout implements
        SelectTermView.SelectTermListener, View.OnClickListener {
    private final int select_school = 1;
    private final int select_term = 2;
    private View mRootView;
    private RelativeLayout view_left;
    private SelectTermView selectTermView;
    private TextView text_school_name;
    private ImageView image_left_arrow;
    private RelativeLayout view_right;
    private TextView text_select_choice;
    private ImageView image_right_arrow;
    private List<SelectEntity> mSchools = new ArrayList<>();
    private List<SelectEntity> selectTerms = new ArrayList<>();
    private SelectEntity leftSelect;
    private SelectEntity rightSelect;
    private SelectListener selectListener;
    private int mType = 1;

    public SelectSchoolTab(Context context) {
        super(context);
        initView(context);
    }

    public SelectSchoolTab(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public SelectSchoolTab(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRootView = inflater.inflate(R.layout.layout_choice_indicator, this);
        view_left = mRootView.findViewById(R.id.view_left);
        view_left.setOnClickListener(this);
        text_school_name = mRootView.findViewById(R.id.text_school_name);
        image_left_arrow = mRootView.findViewById(R.id.image_left_arrow);
        view_right = mRootView.findViewById(R.id.view_right);
        view_right.setOnClickListener(this);
        text_select_choice = mRootView.findViewById(R.id.text_select_choice);
        image_right_arrow = mRootView.findViewById(R.id.image_right_arrow);
        initTermData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_left:
                showSchools();
                mType = select_school;
                break;
            case R.id.view_right:
                showTerms();
                mType = select_term;
                break;
        }
    }

    private void initTermData() {
        List<String> strings = new ArrayList<>();
        strings.add("男生和女生");
        strings.add("男生");
        strings.add("女生");
        setSelectTerms(strings);
    }

    public void setSelectTermView(SelectTermView selectTermView) {
        this.selectTermView = selectTermView;
        this.selectTermView.setSelectTermListener(this);
    }

    public void setSelectListener(SelectListener selectListener) {
        this.selectListener = selectListener;
    }

    /**
     * 设置选择学校
     *
     * @param schools
     */
    public void setSchools(List<SchoolInfoEntity> schools) {
        if (schools.size() > 0) {
            mSchools.clear();
            for (SchoolInfoEntity schoolInfoEntity : schools) {
                SelectEntity selectEntity = new SelectEntity();
                selectEntity.selectName = schoolInfoEntity.getSchool();
                mSchools.add(selectEntity);
            }
            SelectEntity selectEntity = mSchools.get(0);
            text_school_name.setText(selectEntity.selectName);
            text_school_name.setTextColor(getResources().getColor(R.color.str_color_black));
            image_left_arrow.setBackground(getResources().getDrawable(R.mipmap.icon_downarrow));
            leftSelect = selectEntity;
        }
    }

    /**
     * 显示学校
     */
    public void showSchools() {
        showSchools(-1);
    }

    /**
     * 显示学校的选择条件
     * 默认选中位置，-1未都未选中
     */
    public void showSchools(int selectPosition) {
        if (selectTermView != null && mSchools.size() > 0) {
            selectTermView.updateTermName(mSchools, selectPosition);
            selectTermView.setVisibility(VISIBLE);
        }
    }

    public void showSelectSchools(int pos) {
        SelectEntity selectEntity = mSchools.get(pos);
        text_school_name.setText(selectEntity.selectName);
        text_school_name.setTextColor(getResources().getColor(R.color.common_green));
        image_left_arrow.setBackground(getResources().getDrawable(R.mipmap.icon_up_arrow));
        leftSelect = selectEntity;
    }

    public void showSelectSchoolDefault(int pos) {
        SelectEntity selectEntity = mSchools.get(pos);
        text_school_name.setText(selectEntity.selectName);
        text_school_name.setTextColor(getResources().getColor(R.color.str_color_black));
        image_left_arrow.setBackground(getResources().getDrawable(R.mipmap.icon_downarrow));
        leftSelect = selectEntity;
    }

    public void showSelectTerm(int pos) {
        SelectEntity selectEntity = selectTerms.get(pos);
        text_select_choice.setText(selectEntity.selectName);
        text_select_choice.setTextColor(getResources().getColor(R.color.common_green));
        image_right_arrow.setBackground(getResources().getDrawable(R.mipmap.icon_up_arrow));
        rightSelect = selectEntity;
    }

    public void showDefaultTerm(int pos) {
        SelectEntity selectEntity = selectTerms.get(pos);
        text_select_choice.setText(selectEntity.selectName);
        text_select_choice.setTextColor(getResources().getColor(R.color.str_color_black));
        image_right_arrow.setBackground(getResources().getDrawable(R.mipmap.icon_downarrow));
        rightSelect = selectEntity;
    }

    /**
     * 显示选择条件
     */
    public void showTerms() {
        showTerms(-1);
    }

    /**
     * -1都不选中，默认选中selectPosition位置
     * 显示选择条件信息
     */
    public void showTerms(int selectPosition) {
        if (selectTermView != null && selectTerms.size() > 0) {
            selectTermView.updateTermName(selectTerms, selectPosition);
            selectTermView.setVisibility(VISIBLE);
        }
    }

    /**
     * 设置选择条件
     *
     * @param strings
     */
    public void setSelectTerms(List<String> strings) {
        if (strings != null && strings.size() > 0) {
            selectTerms.clear();
            for (String str : strings) {
                SelectEntity selectEntity = new SelectEntity();
                selectEntity.selectName = str;
                selectTerms.add(selectEntity);
            }
            SelectEntity selectEntity = selectTerms.get(0);
            text_select_choice.setText(selectEntity.selectName);
            text_select_choice.setTextColor(getResources().getColor(R.color.str_color_black));
            image_right_arrow.setBackground(getResources().getDrawable(R.mipmap.icon_downarrow));
            rightSelect = selectEntity;
        }
    }

    /**
     * 显示学校默认选中状态
     *
     * @param selectEntity
     */
    public void showSelectSchool(SelectEntity selectEntity) {
        text_school_name.setText(selectEntity.selectName);
        text_school_name.setTextColor(getResources().getColor(R.color.common_green));
        image_left_arrow.setBackground(getResources().getDrawable(R.mipmap.icon_up_arrow));
        leftSelect = selectEntity;
    }


    /**
     * 显示默认状态
     *
     * @param selectEntity
     */
    public void showDefaultSchool(SelectEntity selectEntity) {
        text_school_name.setText(selectEntity.selectName);
        text_school_name.setTextColor(getResources().getColor(R.color.str_color_black));
        image_left_arrow.setBackground(getResources().getDrawable(R.mipmap.icon_downarrow));
    }

    /**
     * 右边条件选中状态
     *
     * @param selectEntity
     */
    private void showSelectTerm(SelectEntity selectEntity) {
        rightSelect = selectEntity;
        text_select_choice.setText(selectEntity.selectName);
        text_select_choice.setTextColor(getResources().getColor(R.color.common_green));
        image_right_arrow.setBackground(getResources().getDrawable(R.mipmap.icon_up_arrow));
    }

    private void showDefaultTerm(SelectEntity selectEntity) {
        text_select_choice.setText(selectEntity.selectName);
        text_select_choice.setTextColor(getResources().getColor(R.color.str_color_black));
        image_right_arrow.setBackground(getResources().getDrawable(R.mipmap.icon_downarrow));
    }


    @Override
    public void selectTerm(SelectEntity selectEntity) {

        if (mType == select_school) {
            if (selectListener != null) {
                selectListener.selectSchool(selectEntity);
            }
            showSelectSchool(selectEntity);
        } else if (mType == select_term) {

            if (selectListener != null) {
                selectListener.selectTerm(selectEntity);
            }
            showSelectTerm(selectEntity);
        }
        selectTermView.setVisibility(GONE);
    }


    public interface SelectListener {
        //选择学校
        void selectSchool(SelectEntity selectEntity);

        //选择筛选条件
        void selectTerm(SelectEntity selectEntity);
    }
}
